package cn.ac.jaas.isc.vegetable.pbms.controller;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import cn.ac.jaas.isc.vegetable.common.core.util.R;
import cn.ac.jaas.isc.vegetable.common.log.annotation.SysLog;
import cn.ac.jaas.isc.vegetable.pbms.api.entity.ArcModel;
import cn.ac.jaas.isc.vegetable.pbms.service.ArcModelService;
import cn.ac.jaas.isc.vegetable.common.excel.annotation.ResponseExcel;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * @author 张高昌
 * @date 2022-05-02 12:52:09
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/arcmodel")
@Api(value = "arcmodel", tags = "管理")
public class ArcModelController {

    private final ArcModelService arcModelService;

    /**
     * 分页查询
     *
     * @param page     分页对象
     * @param arcModel
     * @return
     */
    @ApiOperation(value = "分页查询", notes = "分页查询")
    @GetMapping("/page")
//    @PreAuthorize("@pms.hasPermission('pbms_arcmodel_view')")
    public R getArcModelPage(Page page, ArcModel arcModel) {
        return R.ok(arcModelService.getModelPage(page, arcModel));
    }


    /**
     * 通过id查询
     *
     * @param modelId id
     * @return R
     */
    @ApiOperation(value = "通过id查询", notes = "通过id查询")
    @GetMapping("/{modelId}")
//    @PreAuthorize("@pms.hasPermission('pbms_arcmodel_view')")
    public R getById(@PathVariable("modelId") Integer modelId) {
        return R.ok(arcModelService.getById(modelId));
    }

    /**
     * 新增
     *
     * @param arcModel
     * @return R
     */
    @ApiOperation(value = "新增", notes = "新增")
    @SysLog("新增")
    @PostMapping
//    @PreAuthorize("@pms.hasPermission('pbms_arcmodel_add')")
    public R save(@RequestBody @Valid ArcModel arcModel) {
        return arcModelService.saveModel(arcModel);
    }

    /**
     * 修改
     *
     * @param arcModel
     * @return R
     */
    @ApiOperation(value = "修改", notes = "修改")
    @SysLog("修改")
    @PutMapping
//    @PreAuthorize("@pms.hasPermission('pbms_arcmodel_edit')")
    public R updateById(@RequestBody @Valid ArcModel arcModel) {
        if (arcModel.getModelId() == null) {
            return R.failed("id不能为空");
        }
        return arcModelService.updateModelById(arcModel);
    }

    /**
     * 通过id删除
     *
     * @param modelId id
     * @return R
     */
    @ApiOperation(value = "通过id删除", notes = "通过id删除")
    @SysLog("通过id删除")
    @DeleteMapping("/{modelId}")
//    @PreAuthorize("@pms.hasPermission('pbms_arcmodel_del')")
    public R removeById(@PathVariable Integer modelId) {
        return arcModelService.removeModelById(modelId);
    }


    /**
     * 导出excel 表格
     *
     * @param arcModel 查询条件
     * @return excel 文件流
     */
    @ResponseExcel
    @GetMapping("/export")
//    @PreAuthorize("@pms.hasPermission('pbms_arcmodel_export')")
    public List<ArcModel> export(ArcModel arcModel) {
        return arcModelService.list(Wrappers.query(arcModel));
    }

    /**
     * 通过厂商id获取
     *
     * @param manufacturerCode
     * @return
     */
    @ApiOperation(value = "通过厂商id获取", notes = "通过厂商id获取")
    @GetMapping("/getListByManufacturerId/{manufacturerCode}")
    public R getListByManufacturerId(@PathVariable("manufacturerCode") String manufacturerCode) {
        List<ArcModel> list = arcModelService.list(Wrappers.<ArcModel>query().lambda().eq(ArcModel::getManufacturerCode, manufacturerCode));
        return R.ok(list);
    }
}
